//
//  HomeDaibanCell.swift
//  ZYA
//
//  Created by 邱仙凯 on 2024/12/2.
//  Copyright © 2024 zwb. All rights reserved.
//

import UIKit
import JJCollectionViewRoundFlowLayout_Swift

class HomeDaibanCell: BadgeCollectionViewCell {
    
    lazy var collectionView:UICollectionView = {
        // 1.创建layout
        
        let layout = JJCollectionViewRoundFlowLayout_Swift()
        layout.minimumLineSpacing = 1
        layout.minimumInteritemSpacing = 1
        layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        layout.scrollDirection = .horizontal
        // 2.创建UICollectionView
        let collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)
        collectionView.backgroundColor = .clear
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.register(DaibanCell.self, forCellWithReuseIdentifier: DaibanCell.identifier)
        
     
        return collectionView
    }()
 
   
    convenience required init(coder : NSCoder){
        self.init()
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.initUtil()
    }
    var coWidth:CGFloat!
    var coHeight:CGFloat!
  
    var ddclo:((MenuItem)->())?
    
    var dataGroup = [[MenuItem]]() {
        didSet {
            if dataGroup.count > 1 {
                pageControl.numberOfPage = dataGroup.count

            } else {
                pageControl.numberOfPage = 0

            }
            self.collectionView.reloadData()
        }
    }
    
    
    var pageControlWidth:CGFloat = 35
    var pageControlHeight:CGFloat = 5
    fileprivate lazy var pageControl: KSPageControl = {
        let pageControl = KSPageControl(frame: CGRect(x: 0, y: 0, width: pageControlWidth, height: pageControlHeight))
         return pageControl
    }()
    func initUtil() {
        coWidth = self.ks_width
        coHeight = self.ks_height
        self.backgroundColor = .white
        self.contentView.backgroundColor = .white
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.isPagingEnabled = true
        
        
     
       
        
        self.contentView.addSubview(collectionView)
        contentView.addSubview(pageControl)
       
        collectionView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
          }
        pageControl.snp.makeConstraints { make in
            make.top.equalTo(collectionView.snp.bottom).offset(7)
            make.centerX.equalTo(collectionView)
            make.width.equalTo(pageControlWidth)
            make.height.equalTo(pageControlHeight)
        }
       
  
     
    }
 
}
// MARK: -   代理和数据源
extension HomeDaibanCell:UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
    
    
   
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  
   
        return dataGroup.count
     
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: DaibanCell.identifier, for: indexPath as IndexPath) as! DaibanCell
        
        let oneGroup = dataGroup[indexPath.row]
        cell.itemDarr = oneGroup
        cell.ddclo = { str in
            if let clo = self.ddclo {
                clo(str)
            }
        }
        
        
        return cell
    
    }
//    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
//        if let clo = self.daibanSelect {
//            
//        }
//    }
    
    
    // 设置itemsize
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
   
   
            return CGSize(width: coWidth, height: coHeight)
   
      
   
    }
    // 滑动停止调用方法
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        let offSet = scrollView.contentOffset.x
        let tempIndex = offSet / coWidth
        self.pageControl.currentPage = Int(tempIndex)
    }
    
    func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {
        scrollViewDidEndDecelerating(scrollView)
    }
    
    
}

extension HomeDaibanCell : JJCollectionViewDelegateRoundFlowLayout_Swift{
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, borderEdgeInsertsForSectionAtIndex section: Int) -> UIEdgeInsets {
        return UIEdgeInsets.init(top: -5, left: newModuleLeading, bottom: 12, right: newModuleLeading)
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, configModelForSectionAtIndex section: Int) -> JJCollectionViewRoundConfigModel_Swift {
        
      
            let model = JJCollectionViewRoundConfigModel_Swift.init()
            model.cornerRadius = 8
            model.backgroundColor = UIColor.white
            return model
   
 
    }
}
import UIKit
class GradientLineView: UIView {
    // 数据点
    var dataPoints: [CGFloat] = [0.1, 20, 150, 130, 300, 35, 40]
    var lineColor = UIColor(kshex: "#FF4A4A")
    var startColor = UIColor(kshex: "#33B07C")
    var endColor = UIColor(kshex: "#DFFFDD")
    override func draw(_ rect: CGRect) {
        super.draw(rect)
        
        guard dataPoints.count > 1 else { return } // 确保有足够点来绘制线段
        self.layer.sublayers?.forEach { $0.removeFromSuperlayer() }
        
        // 1. 创建折线的 UIBezierPath
        let linePath = UIBezierPath()
        let pointSpacing = rect.width / CGFloat(dataPoints.count - 1) // 点之间的间距
        let maxValue = CGFloat(dataPoints.max() ?? 0)
        let minValue = CGFloat(dataPoints.min() ?? 0)
        let valueRange = maxValue - minValue
        let scaleY = self.frame.height / valueRange
        for (index, value) in dataPoints.enumerated() {
            let x = CGFloat(index) * pointSpacing
            let y = self.frame.height - (value - minValue) * scaleY

//            let y = rect.height - value // y 值从下往上绘制
            let point = CGPoint(x: x, y: y)
            
            if index == 0 {
                linePath.move(to: point) // 起始点
            } else {
                linePath.addLine(to: point) // 连接到下一个点
            }
        }
        
        // 2. 绘制折线
        lineColor.setStroke() // 设置线条颜色
        linePath.lineWidth = 2
        linePath.stroke()
        
        // 3. 创建渐变图层
        let gradientLayer = CAGradientLayer()
        gradientLayer.colors = [
            
            startColor.cgColor, // 渐变起始颜色
            endColor.cgColor
            // 渐变结束颜色
        ]
        gradientLayer.startPoint = CGPoint(x: 0.5, y: 0) // 渐变从顶部开始
        gradientLayer.endPoint = CGPoint(x: 0.5, y: 1) // 渐变到底部结束
        gradientLayer.frame = rect // 覆盖整个视图
        
        // 4. 创建裁剪路径（折线下方区域）
        let gradientPath = UIBezierPath()
        gradientPath.append(linePath) // 添加折线路径
        
        // 添加底部区域路径
         let lastPoint = linePath.currentPoint
            gradientPath.addLine(to: CGPoint(x: lastPoint.x, y: rect.height)) // 从最后一个点到底部
            gradientPath.addLine(to: CGPoint(x: 0, y: rect.height)) // 从底部右侧到左下角
            gradientPath.close() // 闭合路径
 
        
        // 5. 使用 CAShapeLayer 应用裁剪路径
        let shapeLayer = CAShapeLayer()
        shapeLayer.path = gradientPath.cgPath
        gradientLayer.mask = shapeLayer
        
        // 6. 将渐变图层添加到视图
        self.layer.addSublayer(gradientLayer)
    }
}
